﻿using BluetoothMachine.Domains;
using BluetoothMachine.Entity;
using BluetoothMachine.ExpandHelper;
using BluetoothMachine.GlobalStatics;
using BluetoothMachine.Views;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Unity;

namespace BluetoothMachine
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        //private Views.DialogHosts.StartUpLoad _window;
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //_ = ProductionPoint.FindCount();
            string device = AppDomain.CurrentDomain.BaseDirectory.Substring(0, 1);
            int count = Process.GetProcessesByName("BluetoothMachine").Length;
            string path = $"{AppDomain.CurrentDomain.BaseDirectory}log4net.config";
            if ((count > 1) || (device.GetHardDiskFreeSpace() < 4))
            {
                MessageBox.Show(count > 1 ? $"不可以重复打开软件！！！" : $"{device}硬盘容量过小，请手动清理垃圾或删除旧数据", "Xing丶Lucifer");
                Current.Shutdown();
            }
            if (!File.Exists(path))
            {
                MessageBox.Show("日志配置文件不存在！", "Xing丶Lucifer");
                Current.Shutdown();
            }
            if (!FixedParameter.IsDeBug)
            {
                if (MotorMovements.InitLTDC())
                {
                    LTDMC.dmc_download_configfile(LTDMC.CardID[0], FixedParameter.LTDMC38000);
                    LTDMC.dmc_download_configfile(LTDMC.CardID[1], FixedParameter.LTDMC38001);
                    MotorMovements.BackOrigin();
                }
            }
            if (!Directory.Exists(FixedParameter.ConfigPath)) Directory.CreateDirectory(FixedParameter.ConfigPath);
            if (!Directory.Exists(FixedParameter.InfraredPath)) Directory.CreateDirectory(FixedParameter.InfraredPath);
            log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            //Thread th = new Thread(() =>
            //{
            //    _window = new Views.DialogHosts.StartUpLoad();
            //    _window.ShowDialog();
            //});
            //th.SetApartmentState(ApartmentState.STA);
            //th.Start();
            //Thread.Sleep(5000);
            //_window?.Dispatcher.BeginInvoke(new Action(() => _window.Close()), DispatcherPriority.Input, null);//关闭进度窗体
        }
    }
}
